import Taro, { Component } from '@tarojs/taro'
import { View } from '@tarojs/components'
import { AtButton } from 'taro-ui'

export default class Test extends Component {
  state = {
    address: '',
    map: ''
  }

  async componentDidMount() {
    const { map, searchInfo: address } = await this.loadScript()
    this.setState({
      map
    })
  }

  // 获取定位
  location() {
    const _that = this
    const map = new BMap.Map("container_map");
    let searchInfo = '';
    //获取当前坐标
    new BMap.Geolocation().getCurrentPosition(
      function (r) {
        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
          let currentIp = r.point

          let addr = r.address
          searchInfo += addr.province;
          searchInfo += addr.city;
          searchInfo += addr.district;
          searchInfo += addr.street;
          searchInfo += addr.street_number;
          _that.setState({ address: searchInfo })
        }
      },
      { enableHighAccuracy: true }
    )
  }

  render() {
    return (
      <View className="test_wrapper">
        {this.state.address}
        <AtButton onClick={this.location.bind(this)}>测试</AtButton>
      </View>
    )
  }
}
